package no.awi.wpd.ui.console;

import java.io.FileNotFoundException;
import java.io.FileReader;

import no.awi.wpd.combat.model.Combat;
import no.awi.wpd.combat.service.CombatCalculatorService;
import no.awi.wpd.serialize.SerializeService;
import no.awi.wpd.unit.model.Unit;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class WpdConsole {
	
	private ApplicationContext appContext;
	
	CombatCalculatorService combatCalculatorService;
	
	SerializeService SerializeService;
	
	public WpdConsole() {
        loadClasspathApplicationContext();
        combatCalculatorService = (CombatCalculatorService) appContext.getBean("combatCalculator");
        SerializeService = (SerializeService) appContext.getBean("SerializeService");
    }
	
	public static void main(String[] args) {
		WpdConsole wpdConsole = new WpdConsole();
		try {
        	for (int i = 0; i < args.length; i++) {
                wpdConsole.calculateCombat(args[i]);
        	}
        } catch (FileNotFoundException e) {
        	System.err.println(e.getMessage());
        }
	}

    void calculateCombat(String file) throws FileNotFoundException {
        Combat combat = SerializeService.deserializeCombat(new FileReader(file));
        combatCalculatorService.calculateCombat(combat);
        System.out.println();
        System.out.println("Good guys: " + combat.getGoodGuys().getName());
        System.out.println("Bad guys: " + combat.getBadGuys().getName());
        System.out.print("Chargers: ");
        for (Unit unit : combat.getChargers()) {
            System.out.print(unit.getName() + " ");
        }
        System.out.println();
        System.out.println();
        System.out.println(combat.getBadGuys().getName() + " Wounds Taken:");
        System.out.println(combat.getBadGuysContactZone().getWoundsTaken());
        System.out.println();
        System.out.println(combat.getGoodGuys().getName() + " Wounds Taken:");
        System.out.println(combat.getGoodGuysContactZone().getWoundsTaken());
    }

	private void loadClasspathApplicationContext() {
		appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
}

